#include <stdlib.h>
#include "../HSGlobal.h"
#include "RandomDeathPowerModel.h"

RandomDeathPowerModel::RandomDeathPowerModel() : PowerModel(){

  currentlyDead = false;
  deathModulus = 10000;
  minDeathTicks = 10;
  maxDeathTicks = 1000;
  currDeathTickCount = 0;
  currDeathTickMax = 0;

}

char *RandomDeathPowerModel::Identify(){
  return (char *)"Random Death Power Model";
}

void RandomDeathPowerModel::dumpStats(FILE *f){
  HSLOG(f,"Stats for %s\n",this->Identify());
}

PowerModel::QueryResult *RandomDeathPowerModel::tick(unsigned long currentTick, float powerInWatts){

  return this->queryResult;

}

PowerModel::QueryResult *RandomDeathPowerModel::tick(unsigned long currentTick){

  if( this->currentlyDead ){

    if( this->currDeathTickCount++ == this->currDeathTickMax ){

      this->currentlyDead = false;
      this->currDeathTickCount = 0;
      this->currDeathTickMax = 0;
      this->queryResult->dead = false; 
      return this->queryResult;

    }
    this->queryResult->dead = true;
    return this->queryResult;

  }else{

    if( rand() % this->deathModulus == 0 ){

      this->currentlyDead = true;
      this->currDeathTickCount = 0;
      this->currDeathTickMax = (rand() % this->maxDeathTicks) + this->minDeathTicks;
 
      this->queryResult->dead = true;
      return this->queryResult;

    }

    this->queryResult->dead = false; 

  }

  return this->queryResult;

}

RandomDeathPowerModel::~RandomDeathPowerModel(){


}

extern "C" PowerModel *Create(){

  return new RandomDeathPowerModel();

}
